package com.expense.app.admin.services.job.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.expense.app.admin.common.BaseService;
import com.expense.app.admin.model.dto.job.JobDTO;
import com.expense.app.admin.model.dto.job.JobImportDTO;
import com.expense.app.admin.model.dto.job.JobListDTO;
import com.expense.app.admin.model.params.job.JobImportParam;
import com.expense.app.admin.model.params.job.nengyuanJobs.*;
import com.expense.app.admin.model.params.job.nengyuanJobs.JobsListParam;
import com.expense.app.admin.services.job.NengyuanJobsServ;
import com.expense.common.constant.enums.DeletedStatus;
import com.expense.common.dao.job.NengyuanJobsDao;
import com.expense.common.exception.PlatformException;
import com.expense.common.model.auth.AdminUser;
import com.expense.common.model.job.NengyuanJobs;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 能源
 */
@Service
public class NengyuanJobsServImpl extends BaseService implements NengyuanJobsServ {
    private static final Log log = LogFactory.get();

    @Autowired
    NengyuanJobsDao nengyuanJobsDao;


    @Override
    public JobImportDTO importExcel(JobImportParam param) {

        AdminUser loginUser = getCurrentUser();
        List<Map<String,String>> list = param.getMapList();
        List<Map<String,String>> errorList = new ArrayList<>();
        List<Map<String,String>> errorDataList = new ArrayList<>();
        if(list != null && list.size()>0) {
            if("update".equals(param.getType())){
                nengyuanJobsDao.deleteAll(loginUser.getId());
            }
            List<NengyuanJobs> jobList = new ArrayList<>();
            for (Map<String, String> map : list) {
                int index = list.indexOf(map);
                String sort = map.get("序号");
                String recScope = map.get("招聘范围");
                String recDept = map.get("招聘单位");
                String jobName = map.get("岗位名称");
                String jobNeed = map.get("招聘数量");
                String jobAge = map.get("年龄要求");
                String eduDegree = map.get("学历");
                String major = map.get("专业要求");
                String jobPraQua = map.get("职称及执业资格");
                String jobRank = map.get("职务职级要求");
                String jobDesc = map.get("其他任职条件");
                String jobSpec = map.get("放宽条件");
                String workPlace = map.get("工作地点");
                String applyUrl = map.get("招聘链接");

                //返回错误记录
                Map<String, String> errorData = new HashMap<>();
                errorData.put("sort", sort);
                errorData.put("recScope", recScope);
                errorData.put("recDept", recDept);
                errorData.put("jobName", jobName);
                errorData.put("jobNeed", jobNeed);
                errorData.put("jobAge", jobAge);
                errorData.put("eduDegree", eduDegree);
                errorData.put("major", major);
                errorData.put("jobPraQua", jobPraQua);
                errorData.put("jobRank", jobRank);
                errorData.put("jobDesc", jobDesc);
                errorData.put("jobSpec", jobSpec);
                errorData.put("workPlace", workPlace);
                errorData.put("applyUrl", applyUrl);

                NengyuanJobs entity = new NengyuanJobs();
                // 序号不能为空
                if (StrUtil.isBlank(sort)) {
                    String reason = "序号不能为空";
                    errorList.add(genErrorMap(index, "序号", sort, reason));
                    errorDataList.add(genErrorDataMap(errorData, reason));
                    continue;
                }
                if (StrUtil.isBlank(recScope)) {
                    String reason = "招聘范围不能为空";
                    errorList.add(genErrorMap(index, "招聘范围", recScope, reason));
                    errorDataList.add(genErrorDataMap(errorData, reason));
                    continue;
                }
                if (StrUtil.isBlank(jobName)) {
                    String reason = "岗位名称不能为空";
                    errorList.add(genErrorMap(index, "岗位名称", jobName, reason));
                    errorDataList.add(genErrorDataMap(errorData, reason));
                    continue;
                }
                if (StrUtil.isBlank(applyUrl)) {
                    String reason = "招聘链接不能为空";
                    errorList.add(genErrorMap(index, "招聘链接", applyUrl, reason));
                    errorDataList.add(genErrorDataMap(errorData, reason));
                    continue;
                }

                setBaseInfo(entity);
                entity.setSort(Convert.toInt(sort,99999));
                entity.setRecScope(recScope);
                entity.setRecDept(recDept);
                entity.setJobName(jobName);
                entity.setJobNeed(jobNeed);
                entity.setJobAge(jobAge);
                entity.setEduDegree(eduDegree);
                entity.setMajor(major);
                entity.setJobPraQua(jobPraQua);
                entity.setJobRank(jobRank);
                entity.setJobDesc(jobDesc);
                entity.setJobSpec(jobSpec);
                entity.setWorkPlace(workPlace);
                entity.setApplyUrl(applyUrl);
                jobList.add(entity);
            }
            if(jobList.size() > 0){
                nengyuanJobsDao.saveAll(jobList);
            }
        }
        JobImportDTO dto = new JobImportDTO();
        dto.setTotalCount(list.size());
        dto.setSuccessCount(list.size() - errorList.size());
        dto.setErrorCount(errorList.size());
        dto.setErrorList(errorList);
        dto.setErrorDataList(errorDataList);
        return dto;
    }

    @Override
    public JobListDTO findList(JobsListParam param) {
        Map<String,Object> condition = new HashMap<>();
        condition.put("key",param.getKey());
        PageQuery pageQuery = initPageQuery(param.getPage(),param.getSort());
        pageQuery.setParas(condition);
        nengyuanJobsDao.findListPage(pageQuery);
        return new JobListDTO(pageQuery.getList(),pageQuery.getTotalRow());
    }

    @Override
    public NengyuanJobs findOne(Long id) {
        return nengyuanJobsDao.findByIdAndDeleted(id);
    }

    @Override
    public JobDTO findDetail(Long id) {
        NengyuanJobs entity = nengyuanJobsDao.findByIdAndDeleted(id);
        return new JobDTO(entity);
    }

    @Override
    public NengyuanJobs saveOne(JobsParam param) {
        NengyuanJobs entity = param.getEntity();
        setBaseInfo(entity);
        nengyuanJobsDao.saveOne(entity);
        //其他关联处理
        return entity;
    }

    @Override
    public Long deleteOne(Long id) {
        NengyuanJobs entity = nengyuanJobsDao.findByIdAndDeleted(id);
        if(entity == null) throw new PlatformException("能源已不存在");
        entity.setDeleted(DeletedStatus.DELETED);
        setBaseInfo(entity);
        nengyuanJobsDao.updateTemplateById(entity);
        //其他关联处理
        return id;
    }

    /**
     * 生成错误返回map
     * @param index 错误所在行索引,从0开始
     * @param field 错误列名
     * @param fieldValue 错误列值
     * @param reason 错误说明
     * @return
     */
    public Map<String,String> genErrorMap(Integer index,String field,String fieldValue,String reason){
        Map<String, String> exit = new HashMap<>();
        exit.put("index", (index+1)+"");
        exit.put("field", field);
        exit.put("val",fieldValue);
        exit.put("reason", reason);
        return exit;
    }
    public Map<String,String> genErrorDataMap(Map<String,String> errorData,String reason){
        errorData.put("reason", reason);
        return errorData;
    }
}
